#include "WorldObject.h"
#include "ScreenHandler.h"
#include "WorldHandler.h"

WorldObject::WorldObject()
{
	//Construct additional pylons
	//mesh = passedMesh;
	//position = passedPosition;
	//isInitialized = passedIsInitialized;
	//nodeID = 0;
	isInTown = false;
	node = NULL;
}

WorldObject::~WorldObject()
{
	//keel pylons
}

void WorldObject::setPosition(core::vector3df newPosition)
{
	position = newPosition;
	
	if(node != NULL)
	//&& objectType != ATTACK_OBJECT)
	{
		node->setPosition(newPosition);
	}
}

void WorldObject::setObjectType(unsigned int passedObjectType)
{
	objectType = passedObjectType;
}

core::vector3df WorldObject::getPosition()
{
	return node? node->getAbsolutePosition() : position;
}

unsigned int WorldObject::getObjectType()
{
	return objectType;
}

void WorldObject::faceTarget(core::vector3df targetPos)
{
	if(node != NULL)
	{
		core::vector3df nodePos = targetPos - node->getPosition();
		float degree = atan(nodePos.Z/nodePos.X) * (180.0f / irr::core::PI);
		if((targetPos.X - node->getPosition().X) > 0) 
		{
			degree = 90 - degree;
		} 
		else 
		{
			degree = -90 - degree;
		}
		degree -= 90;
		node->setRotation(core::vector3df(0,(float) degree, 0));

		mat  = node->getRelativeTransformation();
	}
}

scene::IMeshSceneNode* WorldObject::getNode()
{
	return node;
}

void WorldObject::setNode(scene::IMeshSceneNode* passedNode)
{
	node = passedNode;
	boundingBox = node->getTransformedBoundingBox();
	//node->setDebugDataVisible(scene::EDS_BBOX);
}

void WorldObject::setModelName(const std::string& passedName)
{
	modelName = passedName;
}

void WorldObject::setTextureName(const std::string& passedName)
{
	textureName = passedName;
}

const std::string& WorldObject::getTextureName()
{
	return textureName;
}

const std::string& WorldObject::getModelName()
{
	return modelName;
}

bool WorldObject::getIsInTown()
{
	return isInTown;
}

void WorldObject::setIsInTown(bool passedisInTown)
{
	isInTown = passedisInTown;
}

const int WorldObject::getID()
{
	return ID;
}

void WorldObject::setID(const int passedID)
{
	ID = passedID;
}

void WorldObject::setMat(core::matrix4 passedMat)
{
	mat = passedMat;
}

core::matrix4 WorldObject::getMat()
{
	return mat;
}

void WorldObject::setBoundingBox(core::aabbox3df passedBox)
{
	boundingBox = passedBox;
}

core::aabbox3df WorldObject::getBoundingBox()
{
	core::aabbox3df bbox = node->getTransformedBoundingBox(); 
	if(objectType == LIVING_OBJECT
	|| objectType == PLAYER_OBJECT)
	{
		if(reinterpret_cast<LivingObject*>(this)->isDead == true)
		{
			bbox = core::aabbox3df(0, 0, 0, 0, 0, 0);
		}
	}
	return bbox;
}
